<{include file="header.html"}>

<script type="text/javascript">

	function validate()
	{
		var name = document.getElementById('NAME');

		// checking NAME
		if(isEmptyTextBox(name, '<{$smarty.const._MSG_NAME_IS_EMPTY|utf8_escape_js}>'))
			return false;

		if(!isValidLengthTextBox(name, <{$smarty.const.MAX_LENGTH_NAME}>, '<{$smarty.const._MSG_NAME_IS_TOO_LONG|utf8_escape_js}>')){
            return false;
        }

		if ($('#IS_URL').val() == '0') {
			// validate FILE
			// - FILE must be not empty
			// - the extension must is 'pdf', 'doc' or 'docx'
			<{if $product_download->ID}>
			if ($('#FILE').val() != '' && !validateExtension($('#FILE').val(), new Array('pdf','doc','docx'))) {
				showErrorBubble($('#FILE'), '<{$smarty.const._MSG_FILE_EXTENSION_IS_INVALID|utf8_escape_js}>');
				return false;
			}
			<{else}>
			if (!$('#FILE').val() || $('#FILE').val() == '' || !validateExtension($('#FILE').val(), new Array('pdf','doc','docx'))) {
				showErrorBubble($('#FILE'), '<{$smarty.const._MSG_FILE_EXTENSION_IS_INVALID|utf8_escape_js}>');
				return false;
			}
			<{/if}>
		} else {
			// validate URL
			if (!isValidURL($('#URL').val())) {
				showErrorBubble($('#URL'), '<{$smarty.const._MSG_URL_IS_INVALID|utf8_escape_js}>');
				return false;
			}
		}

		document.user_form.submit();
	}

	function validateExtension(fileName, extension)
	{
		var fileExtension = getFileExtension(fileName).toLowerCase();
		if ($.isArray(extension)) {
			for(var i = 0; i < extension.length; i++) {
				if (fileExtension == extension[i].toLowerCase()) {
					return true;
				}
			}
		} else {
			return (fileExtension == extension.toLowerCase());
		}

		return false;
	}

	function getFileExtension(fileName)
	{
		var found = fileName.lastIndexOf('.') + 1;
		return (found > 0 ? fileName.substr(found) : "");
	}

	function isExistProductInfo(name) {
		var args = Array();
		var id_product = <{$product_download->ID_PRODUCT}>;
		var id_product_download = <{if ($product_download->ID)}><{$product_download->ID}><{else}>0<{/if}>;
		args.push(name);
		args.push(id_product);
		args.push(id_product_download);

		var ajaxRes = callFunction('ProductInfo::isExist', args);

		return (ajaxRes == 0 ? false : true);
	}

	$(document).ready(function() {
		<{if $product_download->ID}>
			$('#IS_URL').val('<{$product_download->IS_URL}>');
		<{/if}>
		updateFileUploadVisibility();
	});

	function updateFileUploadVisibility() {
		if ($('#IS_URL').val() == '0') {
			$('#rw_file_upload').show();
			$('#rw_url').hide();
		} else {
			$('#rw_file_upload').hide();
			$('#rw_url').show();
		}
	}

</script>

		<div class="box_two_third">
			<div class="box_head"><{$smarty.const._LB_PRODUCT_MANAGEMENT|utf8_escape_html}></div>
			<div class="box_content">

				<form enctype="multipart/form-data" action="<{$smarty.server.REQUEST_URI}>" method="post" name="user_form">
				<input type="hidden" id="ID" value="<{$product_download->ID}>"/>
				<input type="hidden" id="act" name="act" value="<{$smarty.const.ACT_VALID_EDIT}>"/>
				<table>
					<tr>
						<td><{$smarty.const._LB_NAME|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
						<td colspan="3"><input name="NAME" id="NAME" type="text" size="50" value="<{$product_download->NAME|utf8_escape_html}>" /></td>
					</tr>
                    <tr>
						<td><{$smarty.const._LB_TYPE|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
						<td colspan="3">
							<select name="IS_URL" id="IS_URL" onchange="updateFileUploadVisibility();">
								<option value="0"><{$smarty.const._LB_FILE|utf8_escape_html}></option>
                                <option value="1"><{$smarty.const._LB_URL|utf8_escape_html}></option>
                            </select>
						</td>
					</tr>
                   <tr id="rw_file_upload">
						<td><{$smarty.const._LB_FILE|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
						<td colspan="3">
							<input name="FILE" id="FILE" type="file"/>
                        </td>
					</tr>
					<tr id="rw_url">
						<td><{$smarty.const._LB_URL|utf8_escape_html}>&nbsp;:&nbsp;<{$smarty.const._LB_REQUIRE_FIELD}></td>
						<td colspan="3"><input name="URL" id="URL" type="text" size="50" value="<{if $product_download->IS_URL}><{$product_download->FILE|utf8_escape_html}><{/if}>" /></td>
					</tr>
                    <tr>
						<td><{$smarty.const._LB_DISABLED|utf8_escape_html}>&nbsp;:&nbsp;</td>
						<td colspan="3"><input type="checkbox" value="1" name="DISABLED" <{if $product_download->DISABLED}>checked<{/if}>/></td>
					</tr>
					<tr>
						<td class="submit" colspan="4" style="padding-top:10px"><br />
							<a class="tribullet" href="<{$backlink}>"><{$smarty.const._LB_BACK}></a>
							<a class="tribullet" href="#" onclick="validate()"><{$smarty.const._LB_VALIDATE}></a>
						</td>
					</tr>
				</table>
				</form>
			</div>
		</div>
<{include file="footer.html"}>
